package com.kuangchi.weixin.sginin.controller.Impl;

import com.alibaba.excel.EasyExcel;
import com.kuangchi.weixin.sginin.controller.IDealWIthController;
import com.kuangchi.weixin.sginin.service.IDealWIthService;
import com.kuangchi.weixin.sginin.vo.KcSgininExcelVo;
import com.kuangchi.weixin.sginin.vo.KcSgininVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.kuangchi.weixin.sginin.controller.Impl
 * @类名称：DealWIthController
 * @创建人：YIHANG.XU
 * @创建时间：2020/10/141:34 下午
 * @修改人：YIHANG.XU
 * @修改时间：2020/10/141:34 下午
 * @修改备注：
 * @Copyright
 */
@RestController
@RequestMapping("/dealWIth")
public class DealWIthController implements IDealWIthController {
    @Autowired
    IDealWIthService dealWIthService;

    @Override
    @PostMapping("/userSignUp")
    public String userSignUp(@RequestBody KcSgininVo kcSgininVo) {
        return dealWIthService.userSignUp(kcSgininVo);
    }

    @Override
    @GetMapping("/getUserVo/{openId}")
    public List<KcSgininVo> getUserVo(@PathVariable("openId") String openId) {
        return dealWIthService.getUserVo(openId);
    }

    @Override
    @PostMapping("/userSignIn/{openId}")
    public String userSignIn(@PathVariable("openId") String openId) {
        return dealWIthService.userSignIn(openId);

    }

    @Override
    @GetMapping("/downloadExcel")
    public void downloadExcel(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 保证下载到本地文件名不乱码的
            String fileName = URLEncoder.encode("培训老师签到信息表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            EasyExcel.write(response.getOutputStream(), KcSgininExcelVo.class).sheet("签到信息").doWrite(dealWIthService.getExcelData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    @PostMapping("/getPage")
    public void getPage(@RequestBody Map<String,String> id) {
        System.out.println(id.get("id"));
        dealWIthService.getPage();
    }

    @Override
    @PostMapping("/getParkingCount")
    public String getParkingCount() {
        int count = dealWIthService.getParkingCount();
        return "一共" + count + "人需要停车。";
    }
}
